/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DB;

import java.io.Serializable;
import java.util.Date;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.xml.bind.annotation.XmlRootElement;

/**
 *
 * @author weidas
 */
@Entity
@Table(name = "ChatMessage")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "ChatMessage.findAll", query = "SELECT c FROM ChatMessage c"),
    @NamedQuery(name = "ChatMessage.findById", query = "SELECT c FROM ChatMessage c WHERE c.chatMessagePK.id = :id"),
    @NamedQuery(name = "ChatMessage.findByMessage", query = "SELECT c FROM ChatMessage c WHERE c.message = :message"),
    @NamedQuery(name = "ChatMessage.findByTimesent", query = "SELECT c FROM ChatMessage c WHERE c.timesent = :timesent"),
    @NamedQuery(name = "ChatMessage.findBySenderId", query = "SELECT c FROM ChatMessage c WHERE c.chatMessagePK.senderId = :senderId"),
    @NamedQuery(name = "ChatMessage.findByChatid", query = "SELECT c FROM ChatMessage c WHERE c.chatMessagePK.chatid = :chatid")})
public class ChatMessage implements Serializable {
    private static final long serialVersionUID = 1L;
    @EmbeddedId
    protected ChatMessagePK chatMessagePK;
    @Basic(optional = false)
    @Column(name = "message")
    private String message;
    @Basic(optional = false)
    @Column(name = "timesent")
    @Temporal(TemporalType.TIMESTAMP)
    private Date timesent;
    @JoinColumn(name = "sender_id", referencedColumnName = "id", insertable = false, updatable = false)
    @ManyToOne(optional = false, fetch = FetchType.EAGER)
    private User user;
    @JoinColumn(name = "Chat_id", referencedColumnName = "id", insertable = false, updatable = false)
    @ManyToOne(optional = false, fetch = FetchType.EAGER)
    private Chat chat;

    public ChatMessage() {
    }

    public ChatMessage(ChatMessagePK chatMessagePK) {
        this.chatMessagePK = chatMessagePK;
    }

    public ChatMessage(ChatMessagePK chatMessagePK, String message, Date timesent) {
        this.chatMessagePK = chatMessagePK;
        this.message = message;
        this.timesent = timesent;
    }

    public ChatMessage(int id, int senderId, int chatid) {
        this.chatMessagePK = new ChatMessagePK(id, senderId, chatid);
    }

    public ChatMessagePK getChatMessagePK() {
        return chatMessagePK;
    }

    public void setChatMessagePK(ChatMessagePK chatMessagePK) {
        this.chatMessagePK = chatMessagePK;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Date getTimesent() {
        return timesent;
    }

    public void setTimesent(Date timesent) {
        this.timesent = timesent;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public Chat getChat() {
        return chat;
    }

    public void setChat(Chat chat) {
        this.chat = chat;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (chatMessagePK != null ? chatMessagePK.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof ChatMessage)) {
            return false;
        }
        ChatMessage other = (ChatMessage) object;
        if ((this.chatMessagePK == null && other.chatMessagePK != null) || (this.chatMessagePK != null && !this.chatMessagePK.equals(other.chatMessagePK))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "DB.ChatMessage[ chatMessagePK=" + chatMessagePK + " ]";
    }
    
}
